package com.zc.webnews.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.view.ViewGroup;

import com.zc.webnews.fragment.BaseNews;

import java.util.ArrayList;
import java.util.List;


/**
 * 新闻列表适配器
 * Created by Administrator on 2016/11/02.
 */
public class MyNewsFragmentAdapter extends FragmentPagerAdapter {

    private List<BaseNews> list = new ArrayList<BaseNews>();
    private FragmentManager fm;

    public MyNewsFragmentAdapter(FragmentManager fm) {
        super(fm);
        this.fm = fm;
    }

    public MyNewsFragmentAdapter(FragmentManager fm, List<BaseNews> list) {
        super(fm);
        this.list = list;
        this.fm = fm;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return list.get(position).getTitle();
    }

    public void setFragments(List<BaseNews> list) {
        if(this.list != null){
            FragmentTransaction ft = fm.beginTransaction();
            for(Fragment f:this.list){
                ft.remove(f);
            }
            ft.commit();
            ft = null;
            fm.executePendingTransactions();
        }
        //this.list = list;
        //notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }
}
